package com.lechuang.songbo.view.activity.home;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.preference.PreferenceManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.MemoryCategory;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.lechuang.songbo.R;
import com.lechuang.songbo.model.LeCommon;
import com.lechuang.songbo.model.bean.GetBean;
import com.lechuang.songbo.model.bean.HomeLastProgramBean;
import com.lechuang.songbo.presenter.CommonAdapter;
import com.lechuang.songbo.presenter.net.Netword;
import com.lechuang.songbo.presenter.net.ResultBack;
import com.lechuang.songbo.presenter.net.netApi.HomeApi;
import com.lechuang.songbo.utils.Utils;
import com.lechuang.songbo.view.defineView.SpannelTextView;
import com.lechuang.songbo.view.defineView.ViewHolder;

import java.util.HashMap;
import java.util.List;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Author: guoning
 * Date: 2017/10/2
 * Description:
 */

public class CommFragmentImpl extends CommFragment {


    private Subscription subscribe;
    private int page = 1;
    private ImageSpan imageSpan;
    private ImageSpan imageSpan1;
    private SpannableString ss;

    public CommFragmentImpl() {
        super();
    }


    private CommonAdapter<HomeLastProgramBean.ListBean> mAdapter;
    private int headImg;

    private void getInfoList() {
        final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
        final HashMap<String, String> map = new HashMap<>();
        map.put("page", page + "");
        if (rootId != -1) {  //Integer classTypeId   分类id,精选不传
            map.put("classTypeId", rootId + "");
        } else {             //Integer is_official   精选传1,其他不传
            map.put("is_official", 1 + "");
        }
            showWaitDialog("");
        subscribe = Netword.getInstance().getApi(HomeApi.class)
                .homeLastProgram(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultBack<HomeLastProgramBean>(getActivity()) {
                    @Override
                    public void successed(HomeLastProgramBean result) {

                        if (result == null) return;
                        if (page == 1 && productList != null) {
                            productList.clear();
                        }
                        List<HomeLastProgramBean.ListBean> list = result.productList;
                        productList.addAll(list);
                        if (page > 1 && list.isEmpty()) {
                            Utils.show(getActivity(), "亲,已经到底了~");
                            return;
                        }
                        if (page == 1) {
                            mAdapter = new CommonAdapter<HomeLastProgramBean.ListBean>(getActivity(), productList, R.layout.item_last_program) {
                                @Override
                                public void setData(ViewHolder viewHolder, Object item) {
                                    HomeLastProgramBean.ListBean bean = (HomeLastProgramBean.ListBean) item;
                                    //商品图
                                    Glide.with(context).load(bean.imgs).into((ImageView) viewHolder.getView(R.id.iv_img));
                                    //动态调整滑动时的内存占用
                                    Glide.get(context).setMemoryCategory(MemoryCategory.LOW);
                                    //原价
                                    viewHolder.setText(R.id.tv_oldprice, bean.price + "");
                                    //中划线
                                    ((TextView) viewHolder.getView(R.id.tv_oldprice)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                                    //券后价
                                    viewHolder.setText(R.id.tv_nowprice, "¥" + bean.preferentialPrice);
                                    //商品名称
                                    TextView productName =viewHolder.getView(R.id.spannelTextView);
                                    ss = new SpannableString(bean.name);
                                    if("1".equals(bean.shopType)){
                                        imageSpan1 = new ImageSpan(getActivity(), R.drawable.zhuan_taobao);
                                    }else {
                                        imageSpan1 = new ImageSpan(getActivity(), R.drawable.zhuan_tianmao);
                                    }
                                        ss.setSpan(imageSpan1,0,1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                                    productName.setText(ss);
                                    //赚
                                    if (sp.getInt("isAgencyStatus", 0
                                    ) == 1) {
                                        viewHolder.getView(R.id.tv_get).setVisibility(View.VISIBLE);
                                    } else {
                                        viewHolder.getView(R.id.tv_get).setVisibility(View.GONE);
                                    }
                                    viewHolder.setText(R.id.tv_get, "赚 ¥ " + bean.zhuanMoney);
                                    //销量
                                    viewHolder.setText(R.id.tv_xiaoliang, "已抢" + bean.nowNumber + "件");
                                    //领券减金额
                                    viewHolder.setText(R.id.tv_quanMoney, "领券减" + bean.couponMoney);
                                }
                            };

                            mgv.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                        mgv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                startActivity(new Intent(getActivity(), ProductDetailsActivity.class)
                                        .putExtra("productUrl", productList.get(position).tbPrivilegeUrl)
                                        .putExtra("alipayItemId", productList.get(position).alipayItemId)
                                        .putExtra("nowPrice", productList.get(position).preferentialPrice)
                                        .putExtra("price", productList.get(position).price + ""));
                            }
                        });
                        hideWaitDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                      hideWaitDialog();
                    }
                });
    }

    /**
     * 刷新结束，取消加载框
     */
   public  void refreshEnd() {

    }

    //
    @Override
    protected void onFragmentInvisible() {
        mFirstLoadData = true;

        if (subscribe != null && !subscribe.isUnsubscribed())
            subscribe.unsubscribe();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_comment;
    }

    @Override
    protected void onFragmentVisible() {
        rootId = getArguments().getInt(LeCommon.KEY_ROOT_ID);
        if (productList != null)
            productList.clear();
        page = 1;
        //获取数据
        getInfoList();
    }

    //上拉刷新数据
    public void notifyChildPagerLoadData() {
        if(rootId==-1){
            rootId = getArguments().getInt(LeCommon.KEY_ROOT_ID);
        }
        page ++;
        getInfoList();
    }

    //下拉加载数据
    public void onPullDownToRefreshData(int newPage) {
        rootId = getArguments().getInt(LeCommon.KEY_ROOT_ID);
        page = newPage;
        getInfoList();
    }

    @Override
    public void show(GetBean.ListInfo listInfo, int position) {

    }
}

